﻿Imports System.Xml
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Xml.Serialization
Imports System.Text
Imports WSEntidades

Public Class AMC

#Region "Metodos_Validacion"
    Public Function isValid_Encabezado(ByRef Resp As List(Of RespuestaPremium), _
                                       ByVal AddAMC As AMC71.ComprobanteAddenda) As Boolean
        Dim msgError As String = ""
        Try
            If (AddAMC.requestForPayment.type <> "") Then
                If (AddAMC.requestForPayment.type <> "SimpleInvoiceType") Then
                    msgError &= "El dato type del Encabezado de la addenda AMC tiene un dato incorrecto." & vbCrLf
                End If
            End If
            If (AddAMC.requestForPayment.contentVersion <> "") Then
                If (AddAMC.requestForPayment.contentVersion <> "1.3.1") Then
                    msgError &= "El dato contentVersion del Encabezado de la addenda AMC tiene un dato incorrecto." & vbCrLf
                End If
            End If

            If (AddAMC.requestForPayment.documentStructureVersion <> "AMC7.1") Then
                msgError &= "El dato documentStructureVersion del Encabezado de la addenda AMC tiene un dato incorrecto." & vbCrLf
            End If

            If (msgError = "") Then
                isValid_Encabezado = True
            Else
                isValid_Encabezado = False
                ValidaCFDi.AgregaRespuesta("12", 0, msgError, Resp)
            End If
        Catch ex As Exception
            isValid_Encabezado = False
            ValidaCFDi.AgregaRespuesta("12", 0, ex.Message, Resp)
        End Try
    End Function

#End Region

    Public Function creaXML(ByVal AddAMC As AMC71.ComprobanteAddenda, ByRef Resp As List(Of RespuestaPremium)) As String
        Dim memory_stream As MemoryStream
        Dim xmlNameSpace As XmlSerializerNamespaces
        Dim xmlTextWriter As XmlTextWriter
        Dim xs As XmlSerializer
        Dim stream_reader As StreamReader
        Dim xmlDoc As XmlDocument
        Try
            'SERIALIZAR
            memory_stream = New MemoryStream()
            xmlNameSpace = New XmlSerializerNamespaces()
            xmlNameSpace.Add("", "")
            xmlTextWriter = New XmlTextWriter(memory_stream, Encoding.UTF8)
            xmlTextWriter.Formatting = Formatting.Indented
            xmlTextWriter.Indentation = 4
            xs = New XmlSerializer(GetType(AMC71.ComprobanteAddenda))
            xs.Serialize(xmlTextWriter, AddAMC, xmlNameSpace)
            stream_reader = New StreamReader(memory_stream)
            memory_stream.Seek(0, SeekOrigin.Begin)
            xmlDoc = New XmlDocument
            xmlDoc.LoadXml(stream_reader.ReadToEnd())
            Dim AddendaAMC71 As String = xmlDoc.DocumentElement.OuterXml.Replace("<ComprobanteAddenda>", "").Replace("</ComprobanteAddenda>", "")
            Return AddendaAMC71
            xmlTextWriter.Close()
            memory_stream.Close()
            memory_stream.Dispose()
            stream_reader.Close()
            stream_reader.Dispose()
        Catch ex As Exception
            ValidaCFDi.AgregaRespuesta("12", 0, "Ocurrió un error al generar la Addenda: " & ex.Message, Resp)
            Return Nothing
        Finally
            memory_stream = Nothing
            xmlNameSpace = Nothing
            xmlTextWriter = Nothing
            xs = Nothing
            stream_reader = Nothing
            xmlDoc = Nothing
            AddAMC = Nothing
        End Try
    End Function

#Region "Metodos_Auxiliares"
    Public Function valida_Correo(ByVal sCorreo As String) As Boolean
        Dim re As Regex = New Regex( _
                            "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@" & _
                            "[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\." & _
                            "[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", _
                            RegexOptions.IgnoreCase)
        Dim m As Match = re.Match(sCorreo)
        If m.Captures.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function
#End Region
End Class
